package ui.activity.order.presenter;


import com.google.gson.Gson;

import javax.inject.Inject;

import Bean.AddOrderBean;
import Bean.AddOrderParamBean;
import Bean.FindAddressBean;
import Bean.GetBankcardParamBean;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.mine.ManagerAddressAct;
import ui.activity.order.biz.ConfirmOrderBiz;
import ui.activity.order.contract.ConfirmOrderContract;
import ui.activity.poscontrol.beanmodel.RedBallnumBean;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class ConfirmOrderPresenter implements ConfirmOrderContract.Presenter {

    ConfirmOrderContract.View view;
    ConfirmOrderBiz confirmOrderBiz;

    @Inject
    public ConfirmOrderPresenter(ConfirmOrderContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        confirmOrderBiz = (ConfirmOrderBiz) biz;
    }


    @Override
    public void getAddressList(String id) {
        GetBankcardParamBean g = new GetBankcardParamBean();
        g.setCustomId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(g));
        confirmOrderBiz.getAddressList(requestBody, new BaseBiz.Callback<FindAddressBean.DataBean>() {
            @Override
            public void onSuccess(FindAddressBean.DataBean info) {
                FindAddressBean.DataBean.ItemsBean itemsBean = null;
                if (info.getItems().size() > 0) {
                    for (int i = 0; i < info.getItems().size(); i++) {
                        if (info.getItems().get(i).getIsDefault() == 1) {
                            itemsBean = info.getItems().get(i);
                            break;
                        }
                    }
                }
                view.upAddressUI(itemsBean);
                view.loading(false);
            }

            @Override
            public void onFailure(FindAddressBean.DataBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getRedballNum(String id) {
        confirmOrderBiz.queryRedballNum(id, new BaseBiz.Callback<RedBallnumBean>() {
            @Override
            public void onSuccess(RedBallnumBean info) {
                view.upRedballUI(info);
            }

            @Override
            public void onFailure(RedBallnumBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void toAddress() {
        view.startAct(ManagerAddressAct.class, null);
    }

    @Override
    public void toSubmitOrder(String buyNumber, String posId, String address, String price, String addressId,int type,int tickid,String siteaddresstext) {
        if (StringUtils.isEmpty(address) || StringUtils.isEmpty(addressId)) {
            view.showMsg("请选择地址");
            return;
        }

        if (StringUtils.isEmpty(buyNumber) || StringUtils.isEmpty(posId) || StringUtils.isEmpty(price)) {
            view.showMsg("信息错误请返回商品详情页面，重新选择");
            return;
        }
        AddOrderParamBean a = new AddOrderParamBean();
        a.setModelId(posId);
        a.setCount(buyNumber);
        a.setAddress(address);
        a.setAddressId(addressId);
        a.setSiteAddress(siteaddresstext);
        if(tickid>0) {
            a.getTicketIds().clear();
            a.getTicketIds().add(tickid);
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(a));
        if(type == 1) {
            confirmOrderBiz.addOrder(requestBody, new BaseBiz.Callback<AddOrderBean.DataBean>() {
                @Override
                public void onSuccess(AddOrderBean.DataBean info) {
                    view.showMsg("提交订单成功");
                    view.toPay(info.getId() + "", price);
                    view.loading(false);
                }

                @Override
                public void onFailure(AddOrderBean.DataBean info) {
                    view.loading(false);
                }
            });
        }else{
            confirmOrderBiz.addOrder2(requestBody, new BaseBiz.Callback<AddOrderBean.DataBean>() {
                @Override
                public void onSuccess(AddOrderBean.DataBean info) {
                    view.showMsg("提交订单成功");
                    view.toPay(info.getId() + "", price);
                    view.loading(false);
                }

                @Override
                public void onFailure(AddOrderBean.DataBean info) {
                    view.loading(false);
                }
            });
        }
    }


}
